package com.chenwy.example.learning.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

public class ConstructTest {

    private String member1;

    private int member2;

    protected ConstructTest(String member1){
        this.member1 = member1;
    }

    public <T> ConstructTest(int member2,T t){
        this.member2 = member2;
    }

    @Override
    public String toString() {
        return "ConstructTest{" +
                "member1='" + member1 + '\'' +
                ", member2=" + member2 +
                '}';
    }

    public static void main(String[] args) {

      ConstructTest test = new <String>ConstructTest(1,"sss");
      //访问public构造函数
      Constructor [] constructors = test.getClass().getConstructors();
      for(Constructor constructor : constructors){
          System.out.println("=====================================================================");
          System.out.println(constructor.getName());//构造函数的名称
          System.out.println(constructor.toGenericString());
          System.out.println(constructor.getAnnotatedReceiverType());//
          System.out.println(constructor.getAnnotatedReturnType());
          System.out.println(constructor.getDeclaredAnnotations());
          System.out.println(constructor.getModifiers());
          System.out.println("===============================TypeVariable==============================");
          TypeVariable<Constructor>  [] variables =  constructor.getTypeParameters();
          for(TypeVariable<Constructor> variable : variables){
              System.out.println(variable.getName());
          }
          System.out.println("===============================GenericParameterType==============================");
          Type[] GenericParameterTypes = constructor.getGenericParameterTypes();
          for(Type type : GenericParameterTypes){
              System.out.println(type.getTypeName());
          }
          System.out.println("===============================ParameterType==============================");
          Class [] ParameterTypes =  constructor.getParameterTypes();
          for(Class clazz : ParameterTypes){
              System.out.println(clazz.getName());
          }

          System.out.println("=================================DeclaredField=====================================");
          ConstructTest constructTest = new ConstructTest(1,"");
          Field [] fields = constructTest.getClass().getDeclaredFields();
          for(Field field : fields){
              field.setAccessible(true);
              try {
                  if(field.getGenericType() == Integer.TYPE){
                      field.set(constructTest,3);
                  }else if(field.getGenericType() == String.class){
                      field.set(constructTest,"233");
                  }
              } catch (IllegalAccessException e) {
                  e.printStackTrace();
              }
              System.out.println(field.getName());
          }
          System.out.println(constructTest);

      }

    }
}
